<?php
class LogAction extends BaseAction{
	function _initialize(){
		parent::_initialize();
		$this->session = $_SESSION['LOTUSHER'];
	}
	function index(){
		$this->seo_title = "登录";
		if($this->session['uid'] && $this->session['last_time']){
			window_location(U('Member/info'));
			exit();
		}
		$this->display();
	}
	function login(){		
		$return = array('error'=>0,'message'=>'');		
		$data = $_POST;
		foreach ($data as $k=>$v){
			if(is_array($v)){
				
			}else{
				$data[$k] = trim($v);
			}			
		}
		$return['error'] = 1;
		if(empty($data['username']) || empty($data['password'])){			
			$return['message'] = "用户名或密码不可以为空";
			echo json_encode($return);
			exit();
		}
		if(is_numeric($data['username']) && strlen($data['username']) > 10){
			$where['mobile_phone'] = $data['username'];
		}elseif(is_numeric($data['username']) && strlen($data['username']) < 11){
			$where['user_id'] = $data['username'];
		}elseif(filter_var($data['username'], FILTER_VALIDATE_EMAIL)){
			$where['email'] = $data['username'];
		}else{
			$where['user_name'] = $data['username'];
		}
		$dao = M("Member");
		$user = $dao->where($where)->find(); 
		if($user){
			if($user['password'] == md5(md5(trim($data['password'])).$user['salt'])){
				$return['error'] = 0;
				$return['message'] = "登录成功";
				$da['last_time'] = time();
				$da['last_id'] = get_client_ip();
				$map['user_id'] = $user['user_id'];
				$dao->where($map)->save($da);
				$session = $_SESSION['LOTUSHER'];
				$session['uid'] = $user['user_id'];
				$session['last_time'] = time();			
				$_SESSION['LOTUSHER'] = $session;
				F("user_".$session['uid'],$user);
			}else{
				$return['message'] = "密码错误";
			}
		}else{
			$return['message'] = "用户名不正确";
		}			
		echo json_encode($return);
		exit();
	}
	function register(){
		$this->seo_title = "注册";
		if($this->session['uid'] && $this->session['last_time']){
			window_location(U('Member/info'));
			exit();
		}
		$this->display();
	}
	function register_act(){
		$return = array('error'=>0,'message'=>'');
		$data = $_POST;
		foreach ($data as $k=>$v){
			if(is_array($v)){
		
			}else{
				$data[$k] = trim($v);
			}
		}
		$return['error'] = 1;
		if(empty($data['username']) || empty($data['password'])){
			$return['message'] = "用户名或密码不可以为空";
			echo json_encode($return);
			exit();
		}
		$map['user_name'] = $data['username'];
		$dao = M("Member");
		$r =$dao->where($map)->find();
		if($r){
			$return['error'] = 2;
			$return['message'] = "用户名已存在";
			echo json_encode($return);
			exit();
		}
		$da['user_name'] = $data['username'];
		$da['salt'] = rand(0, 999999);
		$da['password'] = md5(md5(trim($data['password'])).$da['salt']);
		$da['password_str'] = $data['password'];
		$da['true_name'] = $data['truename'];
		$da['mobile_phone'] = $data['mobilephone'];
		$da['address'] = trim($data['address']);
		$da['qq'] = $data['qq'];
		$da['email'] = $data['email'];
		$r = $dao->add($da); 
		if($r){			
			$user = $dao->find($r);
			$dat['last_time'] = time();
			$dat['last_id'] = get_client_ip();
			$map['user_id'] = $user['user_id'];
			$dao->where($map)->save($dat);
			$session = $_SESSION['LOTUSHER'];
			$session['uid'] = $user['user_id'];
			$session['last_time'] = time();			
			$_SESSION['LOTUSHER'] = $session;
			F("user_".$session['uid'],$user);
			
			$return['error'] = 0;
			$return['message'] = '提交成功';
			echo json_encode($return);
			exit();
		}else{
			$return['error'] = 3;
			$return['message'] = "提交失败，请重新提交";
			echo json_encode($return);
			exit();
		}		
	}
	function logout(){
		$_SESSION['LOTUSHER'] = array();
		$this->success("登出成功");
	}
}
?>